package com.lovetemperature.service.impl;

import java.util.List;

import com.lovetemperature.dao.Dao;
import com.lovetemperature.model.GoodSet;
import com.lovetemperature.model.Member;
import com.lovetemperature.model.MemberState;
import com.lovetemperature.model.Order;
import com.lovetemperature.service.OrderService;

public class OrderServiceImpl implements OrderService{
	private Dao<Order> orderDao;

	@Override
	public boolean addOrder(Order order) {
		orderDao.save(order);
		return true;
	}

	public Dao<Order> getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(Dao<Order> orderDao) {
		this.orderDao = orderDao;
	}

	@Override
	public List<Order> findByShopId(Long shopId) {
		return orderDao.findByField("shopId", shopId.toString());
	}

	@Override
	public void calculatePrice(Order order,Member member) {
		GoodSet goodSet = order.getGoodSet();
		double totalPrice = 0;
		totalPrice += goodSet.getGood().getPrice() * goodSet.getCount();
		if(member.getState().equals(MemberState.银卡)){
			totalPrice *= 0.9;	//银卡打九折
		}else if(member.getState().equals(MemberState.金卡)){
			totalPrice *= 0.8;	//金卡打八折
		}
		order.setTotalPrice(totalPrice);
	}

	@Override
	public Order findById(Long orderNum) {
		return orderDao.findById(orderNum);
	}

	@Override
	public void update(Order order) {
		orderDao.update(order);
	}

	@Override
	public void delect(Order order) {
		orderDao.delect(order);
	}

	@Override
	public List<Order> findByMemberId(Long memberId) {
		List<Order> result = orderDao.query("from Order as o where o.customer.memberId = "+memberId.toString() + " and o.finished = false");
		return result;
	}

}
